/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agb.servlets;

import agb.Modelo.TemaSylabus;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sistemas
 */
public class SrvServletDirectivo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String accion = request.getParameter("accion");
            if(accion.equalsIgnoreCase("registroTemas")){
                registroTemas(request, response);
            }else if(accion.equalsIgnoreCase("listaTemas")){
                listaTemas(request, response);
            }else if(accion.equalsIgnoreCase("eliminarTema")){
                eliminarTemas(request, response);
            }
        } finally {            
            out.close();
        }
    }
    
    protected void registroTemas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            TemaSylabus tema = new TemaSylabus();
            tema.setCodCurso(Integer.parseInt(request.getParameter("cmbTema03")));
            tema.setCodNivelGrado(Integer.parseInt(request.getParameter("cmbTema02")));
            tema.setDescripcionTema(request.getParameter("txtDescripcion"));
            tema.setNombreTema(request.getParameter("txtNombreTema"));
            tema.setFechaInicio(request.getParameter("datestart"));
            tema.setFechaFin(request.getParameter("dateend"));            
            out.println(new TemaSylabus().registraTema(tema)?"TEMA REGISTRADO":"TEMA NO REGISTRADO");
        } finally {            
            out.close();
        }
    }
    
    protected void listaTemas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            TemaSylabus tema = new TemaSylabus();
            tema.setCodCurso(Integer.parseInt(request.getParameter("cmbTema03")));
            tema.setCodNivelGrado(Integer.parseInt(request.getParameter("cmbTema02")));    
            List<TemaSylabus> lista = new TemaSylabus().listaTemas(tema);
            if(lista.isEmpty()){
                out.println("<STRONG> NO EXISTEN TEMAS REGISTRADOS </STRONG>");
            }else{
                out.println("<table width='85%'  align='center'   id='box-table-a'>"
                    + "<tr  > <th height='10'  align='center'><strong>Nivel:&nbsp; </strong></th>" + 
                    "<th height='10'  align='center'><strong>Grado:&nbsp; </strong></th>" +
                    "<th height='10'  align='center'><strong>Curso:&nbsp; </strong></th>" +
                    "<th height='10'  align='center'><strong>Tema:&nbsp; </strong></th>" +
                    "<th height='10' colspan='2'   align='center'><strong>Accion&nbsp; </strong></th>"
                    + "</tr>");
                for(TemaSylabus bean:lista){
                    out.println("<tr  class='contenido'> <td height='10'  align='LEFT'>" + bean.getNombreNivel().split(" ")[0] +" </td>");
                    out.println("<td height='10'  align='LEFT'>" + bean.getNombreGrado() +" </td>");
                    out.println("<td height='10'  align='LEFT'>" + bean.getNombreCurso() +" </td>");
                    out.println("<td height='10'  align='LEFT'>" + bean.getNombreTema() +" </td>");
                    out.println("<td height='10'  align='center'><a href='/SistemaWebAGB/ServletSubida?accion=buscaFoto&codFoto='><strong>Modificar</strong></a></td> "
                            + "<td  align='center'><div id='data'><a href=\"javascript:UpdateDiv('/SistemaWebAGB/SrvServletDirectivo?accion=eliminarTema&codTema=" + bean.getCodTema() + "');\" ><strong>Eliminar</strong></a></div></td></tr>");
                }      
                out.print(" </table>");
            }
               
        } finally {            
            out.close();
        }
    }
    
    protected void eliminarTemas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            TemaSylabus tema = new TemaSylabus();
            tema.setCodTema(Integer.parseInt(request.getParameter("codTema")));          
            out.println(new TemaSylabus().eliminarTema(tema.getCodTema())?"TEMA ELIMINADO":"TEMA NO ELIMINADO");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
